<?php
/**
 * Acmephoto functions and definitions.
 *
 * @link https://developer.wordpress.org/themes/basics/theme-functions/
 *
 * @package Acme Themes
 * @subpackage AcmePhoto
 */

/**
 * require int.
 */
$acmephoto_file_directory_init_file_path = trailingslashit( get_template_directory() ).'acmethemes/init.php';
require $acmephoto_file_directory_init_file_path;
add_action('wp_ajax_nopriv_mylike', 'mylike');
add_action('wp_ajax_mylike', 'mylike');
add_action( 'save_post', 'add_mylike_meta' );

add_filter('manage_posts_columns' , 'add_likes_column');
function add_likes_column($columns) {
    return array_merge( $columns, 
              array('likes' => __('likes')) );
}

add_filter('manage_posts_columns' , 'add_score_column');
function add_score_column($columns) {
    return array_merge( $columns, 
              array('scores' => __('scores')) );
}
add_action( 'manage_posts_custom_column' , 'custom_columns', 10, 2 );

function custom_columns( $column, $post_id ) {
	switch ( $column ) {
		case 'likes':
			echo get_post_meta( $post_id, 'mylike', true ); 
			break;
	}
}

add_action( 'manage_posts_custom_column' , 'custom_columns_for_score', 10, 2 );

function custom_columns_for_score( $column, $post_id ) {
	switch ( $column ) {
		case 'scores':
			$args = array(
			    'post_id' => $post_id,   
			     'type' => 'comment',
			     'status'=>'approve'
			);
			$comments = get_comments($args); 
			$score = 0;
			if ($comments) {
				$likewords = array("like","interesting","good","wealth","happy","pretty","beautiful");//can expand
				$dislikewords = array("dislike","bad","poor","ugly","ill-looking","terrible");//can expand
				foreach ( $comments as $comment ) :
				    $t =  $comment->comment_content;
				    $words = explode(" ", $t);
				   	foreach ($likewords as $lw):
				   		if(in_array($lw, $words)){
				   			$score++;
				   		}
				   	endforeach;
				   	foreach ($dislikewords as $dlw):
				   		if(in_array($dlw, $words)){
				   			$score--;
				   		}
				   	endforeach;
				endforeach;
				
			}
			$likenumber = get_post_meta( $post_id, 'mylike', true ); 
			$totalScore = $score + $likenumber;
			echo $totalScore;
			break;
	}
}
/*
add_action( 'admin_menu', 'my_plugin_menu' );


function my_plugin_menu() {
	add_posts_page( 'Likes Status', 'Likes Status', 'manage_options', 'liked-statistics', 'show_likes' );
}


function show_likes() {
	if ( !current_user_can( 'manage_options' ) )  {
		wp_die( __( 'You do not have sufficient permissions to access this page.' ) );
	}

	// The Query
	$args = array();
	$the_query = new WP_Query( );
	
	// The Loop
	if ( $the_query->have_posts() ) {
		echo '<ul>';
		while ( $the_query->have_posts() ) {
			$the_query->the_post();
			echo '<li>' . get_the_title() . '</li>';
		}
		echo '</ul>';
		wp_reset_postdata();
	} else {
		// no posts found
	}
		
}
*/

function mylike(){
    global $wpdb,$post;
    $id = $_POST["um_id"];
    $action = $_POST["um_action"];
    if ( $action == 'mylike'){
	    $raters = get_post_meta($id,'mylike',true);
	    $expire = time() + 99999;
	    setcookie('mylike_'.$id,$id,$expire,'/','localhost',false);
    if (!$raters || !is_numeric($raters)) {
        update_post_meta($id, 'mylike', 1);
    } 
    else {
            update_post_meta($id, 'mylike', ($raters + 1));
        }
   
    echo get_post_meta($id,'mylike',true);
    
    } 
    
    die;
}
function add_mylike_meta($post_id ){
	$raters = get_post_meta($post_id ,'mylike',true);
	if (is_numeric($raters)) {
        update_post_meta($post_id , 'mylike', $raters);
    } else{
    	update_post_meta($post_id , 'mylike', 0);
    }
}

